//
//  Muse_ex_window.swift
//  Muse
//
//  Created by mac on 2022/12/6.
//

import UIKit

extension UIApplication {
    
    /// 获取 Window
    class var window: UIWindow? {
        get {
            if let appDelegate = UIApplication.shared.delegate,
               let lendo_window = appDelegate.window {
                return lendo_window
            }
            return UIApplication.shared.keyWindow
        }
    }
    
    // 当前控制器
    class func topController(controller: UIViewController? = window?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topController(controller: presented)
        }
        return controller
    }
}
